//
//  DUDayButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/4.
//

import Cocoa

/**
 小日历中，某天按钮
 */
class DUDayButton: BaseButton {

    private var mouseInButton = false

    private let button_solar_rect = NSMakeRect(0, 2, 30, 15)
    private let button_lunar_rect = NSMakeRect(0, 16, 30, 12)
    private let button_rest_rect = NSMakeRect(0, 0, 10, 10)
    private let defaultColor = NSColor.white.cgColor
    private let back1Color = CGColor(red: 1, green: 0.89, blue: 0.89, alpha: 1)
    private let back2Color = CGColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
    private let back3Color = CGColor(red: 0.9, green: 0.8, blue: 0.8, alpha: 0.5)
    private let back4Color = CGColor(red: 0.96, green: 0.2, blue: 0.2, alpha: 1)
    private let back5Color = CGColor(red: 0.95, green: 0.95, blue: 0.95, alpha: 1)
    var solar:String?
    var lunar:String?
    var day:DateEntity?
    var mouseDownHandler: ((_ sender: DUDayButton?) -> Void)?
    
    var solarLayer:CATextLayer!
    var lunarLayer:CATextLayer!
    var restLayer:CATextLayer!
    
    var backColor:CGColor?
    
    init(frame frameRect: NSRect, solarValue solar: String, lunarValue lunar: String) {
        super.init(frame: frameRect)
        //init(title: "", target: "", action: #selector(click(_:)))
        initialize(solar: solar, lunar: lunar, title: "")
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize(solar: "", lunar: "", title: self.title)
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize(solar: "", lunar: "", title: self.title)
    }
    
    override func layout() {
        setDefaultLayer()
    }
    
    private func initialize(solar: String, lunar: String, title:String) {
        self.solar = solar
        self.lunar = lunar
        self.title = title
        backColor = self.defaultColor
        self.solarLayer = createLayer(text: solar, size: 12, frame: button_solar_rect)
        self.lunarLayer = createLayer(text: lunar, size: 8, frame: button_lunar_rect)
        self.restLayer = createLayer(text: "班", size: 6, frame: button_rest_rect)
       // self.restLayer.isHidden = true
        self.state = .off
        
        addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
    }
    
    override var wantsUpdateLayer: Bool {
        return true
    }
    
    
    override func updateLayer() {
        if !self.isEnabled {
            return
        }
        if mouseInButton {
            self.layer?.backgroundColor = CGColor(red: 0.45, green: 0.69, blue: 0.86, alpha: 0.3)
            self.layer?.borderWidth = 1
        } else {
            self.layer?.backgroundColor = NSColor.white.cgColor
            self.layer?.borderWidth = 0
        }
        if self.allowsMixedState {
            switch self.state {
                case .on:
                    self.restType = 1
                case .off:
                    self.restType = 2
                case .mixed:
                    let n = self.restType > 3 ? self.restType : 0
                    self.restType = n
                default:
                    print(self.state)
            }
        }
    }
    
    override func mouseEntered(with event: NSEvent) {
        mouseInButton = true
        self.needsDisplay = true
    }
    
    override func mouseExited(with event: NSEvent) {
        mouseInButton = false
        self.needsDisplay = true
    }
    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        if !self.isEnabled {
            return
        }
        //print(self.state)
        //setNextState()
        mouseDownHandler?(self)
    }
    
    
    func setFixedConstraint(width:CGFloat, height: CGFloat) {
        let fixedWidth = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: width)
        let fixedHeight = NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: height)
        self.addConstraints([fixedWidth, fixedHeight])
        
    }
    
    private func setDefaultLayer () {
        self.layer?.addSublayer(restLayer)
        self.layer?.addSublayer(solarLayer)
        self.layer?.addSublayer(lunarLayer)
        self.layer?.cornerRadius = 0
        self.layer?.borderWidth = 0
        //self.layer?.borderColor = NSColor.gray.cgColor
        self.layer?.borderColor = CGColor(red: 0.45, green: 0.69, blue: 0.86, alpha: 1)
        self.layer?.backgroundColor = backColor
    }
    
    private func createLayer(text:String, size:CGFloat, frame: NSRect) -> CATextLayer {
        let s = CATextLayer()
        s.contentsScale = 2
        s.string = text
        s.alignmentMode = .center
        s.fontSize = size
        s.foregroundColor = NSColor.black.cgColor
        s.frame = frame
        return s
    }
    
    @IBInspectable var solarText: String = "" {
        didSet {
            self.solar = solarText
            self.solarLayer.string = solarText
        }
    }
    
    @IBInspectable var lunarText: String = "" {
        didSet {
            self.lunar = lunarText
            self.lunarLayer.string = lunarText
        }
    }
    
    /**
     根据输入类型改变背景颜色
     -- restType:
        -- 1 节假日休假，右上角显示"休"字样
        -- 2 节假日倒休，右上角显示"班"字样
        -- 3 普通节日，无倒休或加班
        -- 4 周末
        -- 5 非本月日期
        -- 其他，正常日期
     */
    @IBInspectable var restType: Int = 0 {
        didSet {
            switch restType {
            case 1:
                self.restLayer.isHidden = false
                self.restLayer.string = "休"
                self.restLayer.foregroundColor = back4Color
                self.backColor = back1Color
                self.solarLayer.foregroundColor = back4Color
                self.layer?.backgroundColor = backColor
            case 2:
                self.restLayer.isHidden = false
                self.restLayer.string = "班"
                self.restLayer.foregroundColor = NSColor.black.cgColor
                self.backColor = back2Color
                self.solarLayer.foregroundColor = NSColor.black.cgColor
                self.layer?.backgroundColor = backColor
            case 3:
                self.restLayer.isHidden = true
                self.backColor = back3Color
                self.solarLayer.foregroundColor = NSColor.black.cgColor
                self.layer?.backgroundColor = backColor
            case 4:
                self.restLayer.isHidden = true
                self.backColor = defaultColor
                self.solarLayer.foregroundColor = back4Color
                self.layer?.backgroundColor = backColor
            case 5:
                self.restLayer.isHidden = true
                self.backColor = back5Color
                self.layer?.backgroundColor = backColor
            default :
                self.restLayer.isHidden = true
                self.backColor = defaultColor
                self.solarLayer.foregroundColor = NSColor.black.cgColor
                self.layer?.backgroundColor = backColor
            }
        }
    }
}
